package person.bangbang.im.Androidgin.Framework;

import java.util.ArrayList;
import java.util.List;
/**
 * a equality of conversation of pidgin.
 * 
 * @author bangbang.song@gmail.com
 */
public class Conversation {
	/* Not typing */
	public static final int STATE_NOT_TYPING = 0;
	/* Currently typing */
	public static final int STATE_TYPING = 1;
	/* Stopped typing momentarily */
	public static final int STATE_TYPED = 2;
	
	public static final int TYPE_UNKNOWN = 0; //	Unknown conversation type.

	public static final int TYPE_IM = 1; //	Instant Message.

	public static final int TYPE_CHAT = 2; //	Chat room.

	public static final int TYPE_MISC = 3; //	A misc.

	public static final int TYPE_ANY = 4; //	Any type of conversation. 
	
	/**
	 *  with which u talk 
	 */
	private List<Buddy> mPartner = new ArrayList<Buddy>();
	/**
	 * whether a room chatting or not.
	 */
	private int mType = TYPE_UNKNOWN;
	private String mTopic;
	
	private int mTypingState;
	private int mTypingTimeOut;
	
	/* notify id for notifying user a message event when she(he) is out */
	private int mNotifyId;
	
	/* chat message history */
	private List<Message> mMessages = new ArrayList<Message>();
	
	private List<OnImEvent> mImEventListeners = new ArrayList<OnImEvent>();
	private List<OnTypingEvent> mTypingListensers = new ArrayList<OnTypingEvent>();
	private List<OnBuddyEvent> mBuddyListenser = new ArrayList<OnBuddyEvent>();
	
	public Conversation(List<Buddy> parnter, int id) {
		mPartner = parnter ;
		mTopic = "";
		mNotifyId = id;
        mType = TYPE_IM;
		mTypingState = STATE_NOT_TYPING;
	}
	public Conversation(List<Buddy> parnter, String topic, int id) {
		mPartner = parnter;
		mTopic = topic;
		mNotifyId = id;
		mType = TYPE_IM;
		mTypingState = STATE_NOT_TYPING;
	}
	public Conversation(Buddy parnter, int id) {
		List<Buddy> l = new ArrayList<Buddy>();
		l.add(parnter);
		mPartner = l;
		mTopic = "";
		mNotifyId = id;
		mType = TYPE_IM;
		mTypingState = STATE_NOT_TYPING;
	}
	public Conversation(Buddy parnter, String topic, int id) {
		List<Buddy> l = new ArrayList<Buddy>();
		l.add(parnter);
		mPartner = l;
		mTopic = topic;
		mNotifyId = id;
		mType = TYPE_IM;
		mTypingState = STATE_NOT_TYPING;
	}
	
	public int getMTypingTimeOut() {
		return mTypingTimeOut;
	}
	public void setMTypingTimeOut(int typingTimeOut) {
		mTypingTimeOut = typingTimeOut;
	}
	public void startTypingTimeOut() {
		throw new RuntimeException("not implements yes");		
	}
	public void stopTypingTimeOut() {
		throw new RuntimeException("not implements yes");		
	}
	public int getMTypingState() {
		return mTypingState;
	}
	public void setMTypingState(int typingState) {
		mTypingState = typingState;
	}
	protected void addIMessage(Message im) {
		mMessages.add(im);
	}
	
	/**
	 * get buddies THOSE u talk with.
	 * @return
	 */
	public List<Buddy> getPartners() {
		return mPartner;
	}

	public List<Message> getIMessages() {
		// TODO Auto-generated method stub
		return mMessages;
	}

	public String getTopic() {
		return mTopic;
	}
	
	public int getNotifyId() {
		return mNotifyId;
	}
	public int getType() {
		return mType;
	}
	public void send(String message){
		throw new RuntimeException("not implements yes");
	}
	public void regOnImListener(OnImEvent l) {
		mImEventListeners.add(l);
	}
	public void unRegOnImListener(OnImEvent l) {
		mImEventListeners.remove(l);
	}
	public void doImSend(Message e) {
		addIMessage(e);
		
		for (OnImEvent event : mImEventListeners) {
			event.onSentIm(e);
		}
	}
	public void doImRcvd(Message m) {
		addIMessage(m);
		
		for (OnImEvent e : mImEventListeners) {
			e.onRcvdIm(m);
		}
	}
	public void doImErr(int code, String detail) {
		for (OnImEvent e : mImEventListeners) {
			e.onErrIm(code, detail);
		}
	}

	public void regTypingListener(OnTypingEvent l) {
		mTypingListensers.add(l);
	}
	public void doTyping() {
		for (OnTypingEvent it : mTypingListensers) {
			it.onTyping();
		}
	}
	public void doTypingStop() {
		for (OnTypingEvent it : mTypingListensers) {
			it.onTypingStop();
		}
	}
	
	/**
	 * whenever a buddy add or rem and so on.
	 *
	 * just validate in chat mode (more partner than 2)
	 */
	public interface OnBuddyEvent {
		
	}
	public interface OnTypingEvent {
		/**
		 * Emitted when a buddy starts typing in a conversation
		 */
		public void onTyping();
		/**
		 * Emitted when a buddy stops typing in a conversation window. 
		 */
		public void onTypingStop();
	}

	public interface OnImEvent {
		// received a IM
		public void onRcvdIm(Message msg);
		// a IM has been sent. arrive??? i don't know.
		public void onSentIm(Message msg);
		// something wrong occur.
		public void onErrIm(int code, String detail);
	}
}
